<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="96px">
      <el-form-item label="关联订单id"
        prop="orderId">
        <el-input v-model="queryParams.orderId"
          placeholder="请输入关联订单id"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员编号"
        prop="custNo">
        <el-input v-model="queryParams.custNo"
          placeholder="请输入会员编号"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员名称"
        prop="custName">
        <el-input v-model="queryParams.custName"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="退款金额"
        prop="refundAmt">
        <el-input v-model="queryParams.refundAmt"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核退过时间"
        prop="confirmDt">
        <!-- <el-input
          v-model="queryParams.confirmDt"
          placeholder="请输入审核退过时间"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-date-picker v-model="queryParams.confirmDt"
          type="date"
          placeholder="请选择审核退过时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          style="width: 215px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递单号"
        prop="billNo">
        <el-input v-model="queryParams.billNo"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"
          @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10"
      class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['api:refund:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['api:refund:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:refund:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['api:refund:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
      :data="refundList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"
        width="55"
        align="center" />
      <el-table-column label="主键ID"
        align="center"
        prop="id"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="帐套号"
        align="center"
        prop="sysAccount"
        min-width="100" />
      <el-table-column label="退款类型"
        align="center"
        prop="refundType"
        min-width="100">
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.refundType == "1"
              ? "退款"
              : scope.row.refundType == "2"
              ? "退货"
              : ""
          }}</span> -->
          <!-- <span>{{ dictDataChange("return_type", scope.row.refundType) }}</span> -->
          <dict-tag :options="dict.type.return_type"
            :value="scope.row.refundType" />
        </template>
      </el-table-column>
      <el-table-column label="申请理由"
        align="center"
        prop="applyRefuse"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="关联订单id"
        align="center"
        prop="orderId"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="关联订单明细ID"
        align="center"
        prop="orderItemId"
        min-width="160"
        show-overflow-tooltip />
      <el-table-column label="会员id"
        align="center"
        prop="custId"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="会员编号"
        align="center"
        prop="custNo"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="会员名称"
        align="center"
        prop="custName"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="退款金额"
        align="center"
        prop="refundAmt"
        min-width="120"
        sortable />
      <el-table-column label="状态"
        align="center"
        prop="status"
        min-width="100">
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.status == "0"
              ? "申请中"
              : scope.row.status == "1"
              ? "通过"
              : scope.row.status == "2"
              ? "拒绝"
              : scope.row.status == "3"
              ? "退款成功"
              : scope.row.status == "4"
              ? "退款失败"
              : ""
          }}</span> -->
          <!-- <span>{{ dictDataChange("return_status", scope.row.status) }}</span> -->
          <dict-tag :options="dict.type.return_status"
            :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审核退过时间"
        align="center"
        prop="confirmDt"
        min-width="160" />
      <el-table-column label="拒绝原因"
        align="center"
        prop="refuseRemark"
        min-width="160"
        show-overflow-tooltip />
      <el-table-column label="快递单号"
        align="center"
        prop="billNo"
        min-width="150" />
      <el-table-column label="操作人"
        align="center"
        prop="opUserNo"
        min-width="120"
        show-overflow-tooltip />
      <el-table-column label="创建时间"
        align="center"
        prop="createDt"
        sortable
        min-width="160" />
      <el-table-column label="备注"
        align="center"
        prop="remark"
        min-width="150"
        show-overflow-tooltip />
      <el-table-column label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="180">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-check"
            v-if="scope.row.status == 0"
            @click="handleCheckOrder(scope.row)">订单审核</el-button>
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:refund:edit']">修改</el-button>
          <el-button size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:refund:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改退款申请单对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="110px">
        <el-form-item label="帐套号"
          prop="sysAccount">
          <el-input v-model="form.sysAccount"
            placeholder="请输入帐套号" />
        </el-form-item>
        <el-form-item label="申请理由"
          prop="applyRefuse">
          <el-input v-model="form.applyRefuse"
            placeholder="请输入申请理由" />
        </el-form-item>
        <el-form-item label="关联订单id"
          prop="orderId">
          <el-input v-model="form.orderId"
            placeholder="请输入关联订单 id" />
        </el-form-item>
        <el-form-item label="关联订单明细ID"
          prop="orderItemId">
          <el-input v-model="form.orderItemId"
            placeholder="请输入关联订单明细ID" />
        </el-form-item>
        <el-form-item label="会员id"
          prop="custId">
          <el-input v-model="form.custId"
            placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="会员编号"
          prop="custNo">
          <el-input v-model="form.custNo"
            placeholder="请输入会员编号" />
        </el-form-item>
        <el-form-item label="会员名称"
          prop="custName">
          <el-input v-model="form.custName"
            placeholder="请输入会员名称" />
        </el-form-item>
        <el-form-item label="退款金额"
          prop="refundAmt">
          <el-input v-model="form.refundAmt"
            placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="审核退过时间"
          prop="confirmDt">
          <!-- <el-input v-model="form.confirmDt" placeholder="请输入审核退过时间" /> -->
          <el-date-picker v-model="form.confirmDt"
            type="date"
            placeholder="请选择审核退过时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @keyup.enter.native="handleQuery"
            style="width: 100%" />
        </el-form-item>
        <el-form-item label="拒绝原因"
          prop="refuseRemark">
          <el-input v-model="form.refuseRemark"
            placeholder="请输入拒绝原因" />
        </el-form-item>
        <el-form-item label="快递单号"
          prop="billNo">
          <el-input v-model="form.billNo"
            placeholder="请输入快递单号" />
        </el-form-item>
        <!-- <el-form-item label="操作人" prop="opUserNo">
          <el-input v-model="form.opUserNo" placeholder="请输入操作人" />
        </el-form-item> -->
        <!-- <el-form-item label="创建时间" prop="createDt">
          <el-input v-model="form.createDt" placeholder="请输入创建时间" />
        </el-form-item> -->
        <el-form-item label="备注"
          prop="remark">
          <el-input v-model="form.remark"
            placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单审核 -->
    <el-dialog title="订单审核"
      :visible.sync="openOrderCheck"
      width="500px"
      append-to-body>
      <el-form ref="formOrderCheck"
        :model="formOrderCheck"
        :rules="rulesOrderCheck"
        label-width="100px">
        <el-form-item label="状态"
          prop="status">
          <el-select v-model="formOrderCheck.status"
            clearable
            placeholder="请选择"
            style="width: 100%">
            <el-option :key="1"
              label="通过"
              :value="1" />
            <el-option :key="2"
              label="拒绝"
              :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="退款金额"
          prop="refundAmt">
          <el-input v-model="formOrderCheck.refundAmt"
            placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="拒绝原因"
          v-if="formOrderCheck.status == 2"
          prop="refuseRemark">
          <el-input type="textarea"
            v-model="formOrderCheck.refuseRemark"
            placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <el-button type="primary"
          @click="submitFormOrderCheck">确 定</el-button>
        <el-button @click="cancelOrderCheck">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改退款金额 -->
    <el-dialog title="修改订单退款金额"
      :visible.sync="openEditReturnAmout"
      width="500px"
      append-to-body>
      <el-form ref="formEditReturnAmout"
        :model="formEditReturnAmout"
        :rules="rules"
        label-width="110px">
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <el-button type="primary"
          @click="submitFormEditReturnAmout">确 定</el-button>
        <el-button @click="cancelEditReturnAmout">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRefund,
  getRefund,
  delRefund,
  addRefund,
  updateRefund,
  sendBill
} from "@/api/djk/orderMgr/safeAfterOrder";

export default {
  name: "Refund",
  dicts: ["return_type", "return_status"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款申请单表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refundType: null,
        orderId: null,
        custNo: null,
        custName: null,
        refundAmt: null,
        status: null,
        confirmDt: null,
        billNo: null,
        createDt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysAccount: [
          { required: true, message: "帐套号不能为空", trigger: "blur" },
        ],
      },
      dictData: {},
      // 售后审核
      openOrderCheck: false,
      formOrderCheck: {},
      rulesOrderCheck: {
        status: [
          { required: true, message: "请选择退款状态", trigger: "change" },
        ],
        refundAmt: [
          { required: true, message: "请输入退款金额", trigger: "blur" },
        ]
      },
      // 修改订单金额
      openEditReturnAmout: false,
      formEditReturnAmout: {}
    };
  },
  created () {
    this.getList();
  },
  methods: {
    // 订单审核
    handleCheckOrder (row) {
      this.formOrderCheck = {
        id: row.id,
        status: null,
        refundAmt: row.refundAmt,
        refuseRemark: null
      }
      this.openOrderCheck = true
    },
    submitFormOrderCheck () {
      this.$refs["formOrderCheck"].validate((valid) => {
        if (valid) {
          sendBill(this.formOrderCheck).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.openOrderCheck = false;
            this.getList();
          });
        }
      });
    },
    cancelOrderCheck () {
      this.openOrderCheck = false
    },

    // 修改售后退款金额
    submitFormEditReturnAmout () {

    },
    cancelEditReturnAmout () {
      this.openEditReturnAmout = false
    },
    /** 查询退款申请单列表 */
    getList () {
      this.loading = true;
      listRefund(this.queryParams).then((response) => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        sysAccount: null,
        refundType: null,
        applyRefuse: null,
        orderId: null,
        orderItemId: null,
        custId: null,
        custNo: null,
        custName: null,
        refundAmt: null,
        status: null,
        confirmDt: null,
        refuseRemark: null,
        billNo: null,
        opUserNo: null,
        createDt: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加退款申请单";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getRefund(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退款申请单";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRefund(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除退款申请单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRefund(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "api/refund/export",
        {
          ...this.queryParams,
        },
        `refund_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
